using SuperMarketManagement.Entity;

namespace SuperMarketManagement.Web.Helpers
{
    public static class HtmlExtensions
    {
        private static string GetSubCategoryListHTML(Category root, string urlFormat)
        {
            string html = "<ul>";
            foreach (Category c in root.Children)
            {
                html += string.Format("<li><a href='{0}'>{1}</a></li>", string.Format(urlFormat, c.AutoId), c.Name);
            }
            return html;
        }

        public static string ToNavigationTree(this Category root, bool createChild, string urlFormat)
        {
            string tree = "";
            int i = 0;//indicate whether to highlight the node or not
            while (root != null)
            {
                i++;
                tree = string.Format("<ul><li><a{0} href='{1}'>{2}</a></li>{3}{4}</ul>",
                        i == 1 ? " class=selected" : "",
                        string.Format(urlFormat, root.AutoId), root.Name, tree,
                        i == 1 && createChild ? GetSubCategoryListHTML(root, urlFormat) : "");
                root = root.Parent;
            }
            return tree;
        }
    }
}